-module(resource).

-export([set_file/1,test/0,write/0]).

set_file(Module) ->
	put(module,Module),	
	ok.

write() ->
	write_erlang().

test() ->
	set_file(resourcec_test),
	write_erlang().

write_erlang() ->
	case get(module) of
		module -> {error,no_module};
		Module -> 
			write_erlang(Module)
	end.

write_erlang(Module) ->
	Name = Module:get_name(),
	DataDes = Module:get_des(),
	create_file(Name,DataDes),
	ok.


create_file(Name,DataDes) ->
	Create = "resource_" ++ erlang:atom_to_list(Name),
	Sql = "DROP TABLE IF EXISTS `" ++ Create ++ "`;\r\nCREATE TABLE `" ++ Create ++ "` (\r\n\t`id` int(11) NOT NULL AUTO_INCREMENT,\r\n\t`pid` int(11) NOT NULL DEFAULT '0',",
	Record = "-include(\"common.hrl\").\r\n-record(" ++ Create ++ ",{id=0,pid",
	Fun = fun({MaskName,_MaskNum},{Sql,Record}) -> {Sql ++ "\r\n\t`" ++ erlang:atom_to_list(MaskName) ++ "` int(11) NOT NULL DEFAULT '0',",
												   Record ++ "=0," ++ erlang:atom_to_list(MaskName)}
		  end,
	{Sql2,Record2} = lists:foldl(Fun, {Sql,Record}, DataDes),
	Sql3 = Sql2 ++ "\r\n\tPRIMARY KEY (`id`),\r\n\tUNIQUE KEY `usr` (`pid`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n",
	Record3 = Record2 ++ "}).",
	
	case file:open(Create ++ ".erl", [write]) of
		{ok, File} ->
			file:write(File, "-module(" ++ Create ++ ").\r\n"),
			file:write(File, "-export([add_resource/3,get_resource/2,set_resource/3]).\r\n"),
			file:write(File, "" ++ Record3 ++ "\r\n"),
			file:write(File, "\r\n"),
			file:write(File, "get_resource(Pid) -> lib_resource:get_resource(" ++ Create ++ ",Pid).\r\n"),
			file:write(File, "put_resource(Rec) -> lib_resource:put_resource(Rec).\r\n"),
			write_add_resource(Create,File,DataDes),	
			write_get_resource(Create,File,DataDes),
			write_set_resource(Create,File,DataDes),
			file:write(File, "\r\n"),	
			file:close(File);		
		_ -> skip
	end,
	
	
	CreateSql = "sql_resource_" ++ erlang:atom_to_list(Name),
	case file:open(CreateSql ++ ".sql", [write]) of
		{ok, FileSql} ->
			file:write(FileSql, Sql3 ++ "\r\n"),
			file:close(FileSql);			
		_ -> skip
	end.


write_add_resource(_Record,File,DataDes) ->
	Fun = fun({ResourceName,ResourceNum},Resource) ->
				  Resource ++ "add_resource(Pid," ++ erlang:atom_to_list(ResourceName) ++ ",Data)->\r\n\tOld = get_resource(Pid," ++ erlang:atom_to_list(ResourceName) ++ 
					  "),\r\n\tNew = if\r\n\t\tOld + Data > " ++ erlang:integer_to_list(ResourceNum) ++ " -> " ++ erlang:integer_to_list(ResourceNum) ++ 
					  ";\r\n\t\ttrue -> Old + Data\r\n\tend,\r\n\tset_resource(Pid," ++ erlang:atom_to_list(ResourceName) ++ ",New);\r\n"
		  end,
	MaskStr = lists:foldl(Fun, "", DataDes),
	file:write(File, MaskStr),
	file:write(File, "add_resource(_Pid,_Name,_Data) -> 0.\r\n").

write_get_resource(Record,File,DataDes) ->
	Fun = fun({ResourceName,_ResourceNum},Resource) ->
				  Resource ++ "get_resource(Pid," ++ erlang:atom_to_list(ResourceName) ++ ") ->\r\n\tcase get_resource(Pid) of\r\n\t\t[#" ++ Record ++ 
					  "{" ++ erlang:atom_to_list(ResourceName) ++ " = Data}] -> Data;\r\n\t\t_ -> 0\r\n\tend;\r\n"
		  end,
	MaskStr = lists:foldl(Fun, "", DataDes),
	file:write(File, MaskStr),
	file:write(File, "get_resource(_Pid,_Name) -> 0.\r\n").


write_set_resource(Record,File,DataDes) ->
	Fun = fun({ResourceName,ResourceNum},Resource) ->
				  Resource ++ "set_resource(Pid," ++ erlang:atom_to_list(ResourceName) ++ ",Data)->\r\n\tNData = if\r\n\t\tData > " ++ erlang:integer_to_list(ResourceNum) ++ 
					  " -> " ++ erlang:integer_to_list(ResourceNum) ++ ";\r\n\t\ttrue -> Data\r\n\tend,\r\n\tcase get_resource(Pid) of\r\n\t\t[Old = #" ++ Record ++ 
					  "{}]-> put_resource(Old#" ++ Record ++ "{" ++ erlang:atom_to_list(ResourceName) ++ " = NData}),NData;\r\n\t\t_ -> 0\r\n\tend;\r\n"
		  end,
	MaskStr = lists:foldl(Fun, "", DataDes),
	file:write(File, MaskStr),
	file:write(File, "set_resource(_Pid,_Name,_Data) -> 0.\r\n").